

mainApp.controller('userController', ['$rootScope','$scope','$http','smartPager','$timeout','$routeParams','$location','$modal',
    function ($rootScope,$scope, $http,smartPager,	$timeout, $routeParams, $location, $modal) {
        $rootScope.selectItemKey='user';
        // log
        $scope.queryParam = {username:null, disabled:"", admin: null};

        var opts = {
            singleSelect: false,
            url: 'job.do?cmd=user$users',
            pageSize:$rootScope.pageSize,
            params:$scope.queryParam,
            idKey:'_id',
            beforeSend:function(){
            },
            success:function(data){
            }
        };
        $scope.userpager = smartPager.init($scope, opts);
        $scope.userpager.load();

        $scope.userQuery=function(){
            $scope.userpager.load($scope.queryParam);
        };

        $scope.enableUser=function(username){
            if (confirm('确认启用此帐号吗？')) {
                $http.post('job.do?cmd=user$enableUser',{username:username},postCfg).success(function(data){
                    if (data.success == true || data.success == 'true') {
                        alert('操作成功');
                    } else {
                        alert('操作失败，' + data.message);
                    }
                    $scope.userQuery();
                });
            }
        };

        $scope.disableUser=function(username){
            if (confirm('确认禁用此帐号吗？')) {
                $http.post('job.do?cmd=user$disableUser',{username:username},postCfg).success(function(data){
                    if (data.success == true || data.success == 'true') {
                        alert('操作成功');
                    } else {
                        alert('操作失败，' + data.message);
                    }
                    $scope.userQuery();
                });
            }
        };

        $scope.openPwdDialog = function (username) {

            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'pages/user/updatePwd.jsp',
                controller: 'userPwdController',
                resolve: {
                    params:function () { return {username:username}; }
                }
            });

            modalInstance.result.then(function (refresh) {
                if (refresh) $scope.userQuery();
            });
        };

        $scope.openAddDialog = function () {

            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'pages/user/addUser.jsp',
                controller: 'userAddController',
                resolve: {
                    params:function () { return {isEdit:false}; }
                }
            });

            modalInstance.result.then(function (refresh) {
                if (refresh) $scope.userQuery();
            });
        };

        $scope.openUpdateDialog = function (username) {

            var modalInstance = $modal.open({
                animation: true,
                templateUrl: 'pages/user/addUser.jsp',
                controller: 'userUpdateController',
                resolve: {
                    params:function () { return {isEdit:true, username:username}; }
                }
            });

            modalInstance.result.then(function (refresh) {
                if (refresh) $scope.userQuery();
            });
        };
    }]);

mainApp.controller('userPwdController', ['$rootScope','$scope','$http','$timeout','$routeParams','$location', '$modalInstance', 'params',
    function ($rootScope,$scope, $http, $timeout, $routeParams, $location, $modalInstance, params) {
        $rootScope.selectItemKey='user';
        $scope.user = {username:params.username, password:""};

        $scope.updatePwd = function() {
            var reg = new RegExp("^(.){6,16}$");

            if (!reg.test($scope.user.password)) {
                alert('密码必须由6-16个字符组成');
                return;
            }

            if (confirm("确认保存？")) {
                $http.post('job.do?cmd=user$updatePwd', {
                    user : JSON.stringify($scope.user)
                }, postCfg).success(function(data) {
                    if (data.success == 'true' || data.success == true) {
                        alert('保存成功');
                    } else {
                        alert("保存失败：" + data.message);
                    }
                    $modalInstance.close(true);
                });
            }
        };

        $scope.returnPage = function() {
            $modalInstance.dismiss('cancel');
        };

    }]);

mainApp.controller('userAddController', ['$rootScope','$scope','$http','$timeout','$routeParams','$location', '$modalInstance', 'params',
    function ($rootScope,$scope, $http, $timeout, $routeParams, $location, $modalInstance, params) {
        $rootScope.selectItemKey='user';
        $scope.user = {username:"", password:"", disabled:"false", admin:"false", remark:""};
        $scope.isEdit = params.isEdit;
        $scope.saveUser = function() {
            var reg = new RegExp("^(.){6,16}$");

            if (!reg.test($scope.user.password)) {
                alert('密码必须由6-16个字符组成');
                return;
            }

            if (confirm("确认保存？")) {
                $http.post('job.do?cmd=user$saveUser', {
                    user : JSON.stringify($scope.user)
                }, postCfg).success(function(data) {
                    if (data.success == 'true' || data.success == true) {
                        alert('保存成功');
                    } else {
                        alert("保存失败：" + data.message);
                    }
                    $modalInstance.close(true);
                });
            }
        };

        $scope.returnPage = function() {
            $modalInstance.dismiss('cancel');
        };

    }]);

mainApp.controller('userUpdateController', ['$rootScope','$scope','$http','$timeout','$routeParams','$location', '$modalInstance', 'params',
    function ($rootScope,$scope, $http, $timeout, $routeParams, $location, $modalInstance, params) {
        $rootScope.selectItemKey='user';
        $scope.isEdit = params.isEdit;

        $http.post('job.do?cmd=user$getUserByUsername', {
            username : params.username
        }, postCfg).success(function(data) {
            data.admin = String(data.admin);
            data.disabled = String(data.disabled);
            $scope.user = data;
        });

        $scope.saveUser = function() {

            if (confirm("确认保存？")) {
                $http.post('job.do?cmd=user$updateUser', {
                    user : JSON.stringify($scope.user)
                }, postCfg).success(function(data) {
                    if (data.success == 'true' || data.success == true) {
                        alert('保存成功');
                    } else {
                        alert("保存失败：" + data.message);
                    }
                    $modalInstance.close(true);
                });
            }
        };

        $scope.returnPage = function() {
            $modalInstance.dismiss('cancel');
        };

    }]);



mainApp.controller('updatePwd4selfController', ['$rootScope','$scope','$http','$timeout','$routeParams','$location', '$modalInstance', 'params',
    function ($rootScope,$scope, $http, $timeout, $routeParams, $location, $modalInstance, params) {

        $scope.user = {oldPassword:"", password:"", cpassword:""};

        $scope.updatePwd = function() {
            var reg = new RegExp("^(.){6,16}$");

            if ($scope.user.cpassword != $scope.user.password){
                alert('两次密码输入不一致');
                return;
            }

            if (!reg.test($scope.user.password)) {
                alert('密码必须由6-16个字符组成');
                return;
            }

            if (confirm("确认保存？")) {
                $http.post('job.do?cmd=user$updatePwdBySelf', $scope.user, postCfg).success(function(data) {
                    if (data.success == 'true' || data.success == true) {
                        alert('保存成功');
                        $modalInstance.close(true);
                    } else {
                        alert("保存失败：" + data.message);
                    }
                });
            }
        };

        $scope.returnPage = function() {
            $modalInstance.dismiss('cancel');
        };
}]);
